Simple Email Sevice

  • STEPS

    1. SES setup

    1. go to https://eu-central-1.console.aws.amazon.com/ses/home?region=eu-central-1#/get-set-up

    2. create identity

    2. Verify Domain and Email

    1. Add the DNS records in the Hosting Provider Server (here we use AWS Route 53)

    2. go to https://us-east-1.console.aws.amazon.com/route53/v2/hostedzones?region=eu-central-1#ListRecordSets/Z0209881JHOV0P4MRZI6

    3. Add DNS

    3. SMTP settings

  • CODE
    
    
                     
    
    namespace FoodExplorer\ServiceCloud\Models;
    
    use FoodExplorer\ServiceCloud\Models\User;
    use FoodExplorer\ServiceCloud\Models\Credentials;
    use \PHPMailer\PHPMailer\PHPMailer;
    use \PHPMailer\PHPMailer\SMTP;
     
    class Email {
        
        private static $subject = '';
        private static $message = '';
        private static $toEmail = '';
        private static $toEmailLabel = '';
    
    
    
        public static function SendVerificationMail($data){
    
            ob_start();
            require_once 'src/foodexplorer/servicecloud/Templates/Email/emailVerification.php';
            self::$message=ob_get_contents();
            ob_clean(); 
    
            self::$toEmailLabel=$data['firstName']." ".$data['lastName'];
            self::$toEmail=$data['toEmail'];
            self::sendEmail();
        }
    
    
    
    
        public static function sendEmail(){    
            
            $ses_data=Credentials::getSesEmailData();
            //print_r($ses_data);
    
            $ses_data['ses_sender_email']="volker.pischel@eismann.de";
            $ses_data['ses_sender_name']="volker";
            $ses_data['ses_username']="AKIARZQDBTHSV2IMB4XQ";
            $ses_data['ses_password']="BBq/NjwVJjuq2bYU17OqjmenCxtgtlzwPxOqUE5HZ+l2";
            $ses_data['ses_host']="email-smtp.eu-central-1.amazonaws.com";
            
    
            $mail = new \PHPMailer;
            $mail->isSMTP();
            $mail->SMTPDebug   = 2;
            $mail-> SMTPAuth = true;
    
            // Replace sender@example.com with your "From" address. 
            // This address must be verified with Amazon SES.
            $mail->setFrom($ses_data['ses_sender_email'], $ses_data['ses_sender_name']);
    
            // Replace recipient@example.com with a "To" address. If your account 
            // is still in the sandbox, this address must be verified.
            // Also note that you can include several addAddress() lines to send
            // email to multiple recipients.
            $mail->addAddress(self::$toEmail, self::$toEmailLabel);
            //$mail->addBCC("manojvijayanaluva@gmail.com", "Manoj Vijayan");
    
            // Replace smtp_username with your Amazon SES SMTP user name.
            $mail->Username = $ses_data['ses_username'];
    
            // Replace smtp_password with your Amazon SES SMTP password.
            $mail->Password = $ses_data['ses_password'];
                
            // Specify a configuration set. If you do not want to use a configuration
            // set, comment or remove the next line.
            //$mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
            
            // If you're using Amazon SES in a region other than US West (Oregon), 
            // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP  
            // endpoint in the appropriate region.
            $mail->Host = $ses_data['ses_host'];
    
            // The subject line of the email
            $mail->Subject = self::$subject;
    
            // The HTML-formatted body of the email
            $mail->Body =self::$message;
    
            // Tells PHPMailer to use SMTP authentication
            $mail->SMTPAuth = true;
    
            // Enable TLS encryption over port 587
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
    
            // Tells PHPMailer to send HTML-formatted email
            $mail->isHTML(true);
    
            // The alternative email body; this is only displayed when a recipient
            // opens the email in a non-HTML email client. The \r\n represents a 
            // line break.
            
            $mail->AltBody = self::$message;
    
            if(!$mail->send()) {
                echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
            } else {
                echo "Email sent!" , PHP_EOL;
            }
            
            
        }
     
    }